package cn.acewill.yxd.yunxundian.ui.schedule;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.adapter.TimeSelectAdapter;
import cn.acewill.yxd.yunxundian.pubClass.AsynData;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.utils.SpinnerPopWin;

/**
 * Created by XXX on 2017/5/27.
 */

public class CopyTaskActivity extends BaseActivity implements View.OnClickListener{

    private TextView yearSelect,monthSelect,ToyearSelect,TomonthSelect;
    private List<String> yearList;
    private List<String> monthList;
    private Toolbar mytoolbar;
    //选择复制的月（1-12个月）
    private List<String> norMonthList;
    /*当前的时间*/
    private int mYear,mMonth;
    //选择后的年月
    private int syear=0,smonth=0,tyear=0,tmonth=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.copy_scheduleview);
        initView();
        getCurrentTime();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.muti_select_ok,menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_ok:
                LoadData();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void initView(){
        mytoolbar= (Toolbar) findViewById(R.id.myToolBar);
        mytoolbar.setTitle("");
        mytoolbar.setNavigationIcon(R.mipmap.back);
        setSupportActionBar(mytoolbar);
        mytoolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        this.yearSelect= (TextView) findViewById(R.id.yearSelect);
        yearSelect.setOnClickListener(this);
        this.monthSelect= (TextView) findViewById(R.id.monthSelect);
        monthSelect.setOnClickListener(this);
        this.TomonthSelect= (TextView) findViewById(R.id.TomonthSelect);
        TomonthSelect.setOnClickListener(this);
        this.ToyearSelect= (TextView) findViewById(R.id.ToyearSelect);
        ToyearSelect.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.monthSelect:
               getMonthData();
               break;
           case R.id.yearSelect:
               getYearData();
               break;
           case R.id.TomonthSelect:
               getToMonthData();
               break;
           case R.id.ToyearSelect:
               getToYeardata();
               break;
       }
    }

    public void getCurrentTime(){
        Date date=new Date();
        SimpleDateFormat SDF_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM", Locale.getDefault());
        String time=SDF_YYYY_MM_DD.format(date);
        String[] array=time.split("-");
        mYear=Integer.parseInt(array[0]);
        mMonth=Integer.parseInt(array[1]+1);
    }



    //yearlist设置数据源
    public void getYearData(){
        yearList=new ArrayList<>();
        yearList.add(mYear-1+"");
        yearList.add(mYear+"");
        yearList.add(mYear+1+"");
        yearList.add(mYear+2+"");
        TimeSelectAdapter adapter=new TimeSelectAdapter(this,yearList);
        final ListView yearListView=new ListView(this);
        yearListView.setBackgroundColor(ContextCompat.getColor(CopyTaskActivity.this,R.color.white));
        yearListView.setAdapter(adapter);
        final SpinnerPopWin popWin=SpinnerPopWin.getInstance(this,yearListView);
        popWin.showPopupWindow(yearSelect);
        yearListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popWin.dismiss();
                 syear=Integer.parseInt(yearList.get(position));
                yearSelect.setText(syear+"年");
            }
        });
    }

    public void getMonthData(){
        monthList=new ArrayList<>();
        for (int i=1;i<13;i++){
            monthList.add(i+"");
        }
        TimeSelectAdapter adapter=new TimeSelectAdapter(this,monthList);
        ListView monthListView=new ListView(this);
        monthListView.setBackgroundColor(ContextCompat.getColor(this,R.color.white));
        monthListView.setAdapter(adapter);
        final SpinnerPopWin popWin= SpinnerPopWin.getInstance(this,monthListView);
        popWin.showPopupWindow(monthSelect);
        monthListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popWin.dismiss();
                smonth=Integer.parseInt(monthList.get(position));
                monthSelect.setText(smonth+"月");
            }
        });
    }


    public void getToYeardata(){
        //时间要大于当前时间
        yearList=new ArrayList<>();
        yearList.add(mYear+"");
        yearList.add(mYear+1+"");
        yearList.add(mYear+2+"");
        ListView toyearList=new ListView(this);
        toyearList.setBackgroundColor(ContextCompat.getColor(this,R.color.white));
        TimeSelectAdapter adapter=new TimeSelectAdapter(this,yearList);
        toyearList.setAdapter(adapter);
        final SpinnerPopWin popWin= SpinnerPopWin.getInstance(this,toyearList);
        popWin.showPopupWindow(ToyearSelect);
        toyearList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popWin.dismiss();
                 tyear=Integer.parseInt(yearList.get(position));
                //判断年份是否小于选择复制的年份
                if (tyear>=syear){
                    ToyearSelect.setText(tyear+"年");
                }else{
                    BaseActivity.showAnimaDialog(CopyTaskActivity.this,"请选择大于复制日程的时间");
                    tyear=0;
                    ToyearSelect.setText("");

                }
            }
        });
    }
    public void getToMonthData(){
        monthList=new ArrayList<>();
        if (syear==0|| tyear==0){
            //全部
            for (int i=1;i<13;i++){
                monthList.add(i+"");
            }
        }
        else if (syear!=0&&tyear!=0){
            if (syear==tyear){
                if (smonth!=0){
                    for (int i=smonth;i<13;i++){
                        monthList.add(i+"");
                    }
                }else{
                    for (int i=1;i<13;i++){
                        monthList.add(i+"");
                    }
                }
            } else if (tyear>syear) {
                for (int i=1;i<13;i++){
                    monthList.add(i+"");
                }
            }
        }
        TimeSelectAdapter adapter=new TimeSelectAdapter(this,monthList);
        ListView monthListView=new ListView(this);
        monthListView.setBackgroundColor(ContextCompat.getColor(this,R.color.white));
        monthListView.setAdapter(adapter);
        final SpinnerPopWin popWin= SpinnerPopWin.getInstance(this,monthListView);
        popWin.showPopupWindow(TomonthSelect);

        monthListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popWin.dismiss();
                tmonth=Integer.parseInt(monthList.get(position));
                TomonthSelect.setText(tmonth+"月");
            }
        });
    }

    /**
     * 上传数据，先判断数据
     */
    public void LoadData(){
        if (syear==0||smonth==0||tyear==0||tmonth==0){
            BaseActivity.showAnimaDialog(this,"请选择具体的时间");
        }else{
            //数据提交
            Map<String,Object> map=new HashMap<>();
            map.put("empid", MainActivity.id);
            map.put("task","copySchByEmpid");
            map.put("fromyear",syear);
            map.put("frommonth",smonth);
            map.put("toyear",tyear);
            map.put("tomonth",tmonth);
            pubClass.Post(this, pubClass.Server_namehzm,map,new pubClass.MyCallBack<String>(){

                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    String[] ress=result.split("\\|");
                    if (ress[0].equals("200")){
                        AsynData ad=new AsynData(null);
                        ad.asynScheduleData(result);
                        BaseActivity.showAnimaDialog(CopyTaskActivity.this,"复制成功");
                        //更新日程的数据
                        setResult(102);
                        finish();
                    }else{
                        BaseActivity.showAnimaDialog(CopyTaskActivity.this,ress[1]);
                    }
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    super.onError(ex, isOnCallback);
                    BaseActivity.showAnimaDialog(CopyTaskActivity.this,"提交失败，请检查网络状态");
                }
            });

        }

    }

}
